//
//  RigthHomeViewController.m
//  ASHT_IOS
//
//  Created by 宋安安 on 14-4-2.
//  Copyright (c) 2014年 宋安安. All rights reserved.
//

#import "RigthHomeViewController.h"
#import "RigthHomeCell.h"
#import "WebContentViewController.h"
@interface RigthHomeViewController ()

@end

@implementation RigthHomeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
//        self.view.backgroundColor=[UIColor yellowColor];
        _Adaptation=YES;
        self.delegate=self;
     [self InitializationDataSource];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
   
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
    {
        if (_Adaptation) {
            self.view_Content.frame=CGRectMake(self.view_Content.frame.origin.x,self.view_Content.frame.origin.y,self.view_Content.frame.size.width,self.view_Content.frame.size.height-20);
            _Adaptation=NO;
        }
        
    }

    
    [self InitializationDataSource];
    [table_view reloadData];
    
}
-(void)InitializationDataSource
{
    NSString *plistPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
    plistPath =[NSString stringWithFormat:@"%@/%@",plistPath,@"Favorites.plist"];
    NSMutableArray *data = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
    if (data.count==0){
        arr_Favorites_key=[[NSMutableArray alloc] init];
        arr_Favorites=[[NSMutableArray alloc] init];
        data=[[NSMutableArray alloc] init];
        [data writeToFile:plistPath atomically:YES];
    }
    else
    {
        arr_Favorites_key=[NSMutableArray array];
        for (NSMutableDictionary *dic in data) {
            NSArray *arr= [dic allKeys];
            NSString *str=[arr objectAtIndex:0];
            [arr_Favorites_key addObject:str];
        }
        arr_Favorites=[[NSMutableArray alloc]initWithArray:data];
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return arr_Favorites_key.count;
//    return 15;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	//NSLog(@"====in 设置每行的数据=====分区＝%d 行＝%d",indexPath.section,indexPath.row);
	
	static NSString *CellIdentifier = @"Cell";
	
	RigthHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
	{
        NSArray *nibs=[[NSBundle mainBundle] loadNibNamed:@"RigthHomeCell" owner:self options:nil];
        if (nibs.count>0) {
            cell =[nibs objectAtIndex:0];
        }
        
    }
    
    
	//行是否允许点击事件
	tableView.allowsSelection=YES;
	//行选中的样式
//    cell.selectionStyle=UITableViewCellSelectionStyleNone;

    [cell set_lab_title:[arr_Favorites_key objectAtIndex:indexPath.row ]];
    return cell;
}
//选中行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str=[arr_Favorites_key objectAtIndex:indexPath.row ];
    NSString *str1=[[arr_Favorites objectAtIndex:indexPath.row] objectForKey:str];
    WebContentViewController *web=[[WebContentViewController alloc] initWithNibName:@"WebContentViewController" bundle:nil];
    web.titleStr = str;
    web.requestStr = str1;
    web.key=str;
    [self.app.viewDeckController.navigationController pushViewController:web animated:YES];
}
-(BOOL)addFavorites:(NSMutableDictionary *)dic
{
    if (dic==nil) {
        return NO;
    }
    NSString *plistPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
    plistPath =[NSString stringWithFormat:@"%@/%@",plistPath,@"Favorites.plist"];
    [arr_Favorites addObject:dic];
   return [arr_Favorites writeToFile:plistPath atomically:YES];
    
}
-(BOOL)deleteFavorites:(NSMutableDictionary *)dic
{
    NSString *plistPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
    plistPath =[NSString stringWithFormat:@"%@/%@",plistPath,@"Favorites.plist"];
    NSArray *arr_key=[dic allKeys];
    NSString *str_key=[arr_key objectAtIndex:0];
    for (NSMutableDictionary *dic_itme in arr_Favorites) {
        NSArray *arr=[dic_itme allKeys];
        NSString *str=[arr objectAtIndex:0];
        if ([str isEqualToString:str_key]) {
            [arr_Favorites removeObject:dic_itme];
            break;
        }
    }
   return [arr_Favorites writeToFile:plistPath atomically:YES];
}
-(BOOL)DetermineWhetherIsAlready:(NSString *)str_key
{
    for (NSString *str in arr_Favorites_key) {
        if ([str isEqualToString:str_key]) {
            return YES;
        }
    }
    return NO;
}
@end
